//
//  AddPersonVC.m
//  FamilyTree
//
//  Created by student on 16/4/12.
//  Copyright © 2016年 Five. All rights reserved.
//
#import "def.h"
#import "AddPersonVC.h"
#import "PeopleInfoCell.h"
#import "PeopleDataManager.h"
#import "People.h"

#import <HHAlertView.h>
@interface AddPersonVC ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *infoTableView;
@property (nonatomic,strong) NSMutableArray *peopleCellArray;//单元格样式数组
//@property(nonatomic,strong) NSMutableArray *peopleInfo;//存放人物信息
@property(nonatomic,strong) NSMutableDictionary *tempDict;
@property (weak, nonatomic) IBOutlet UIView *infoView;

@end

@implementation AddPersonVC

//单元格样式数组
- (NSMutableArray *)peopleCellArray{
    if (_peopleCellArray == nil) {
        NSDictionary *d1 = @{@"title":@"姓名:",@"placeholder":@"姓名必填"};
        NSDictionary *d2 = @{@"title":@"字号:",@"placeholder":@"可输入字号"};
        NSDictionary *d3 = @{@"title":@"生卒:",@"placeholder":@"可输入生卒"};
        NSDictionary *d4 = @{@"title":@"祖籍:",@"placeholder":@"可输入祖籍"};
        _peopleCellArray = [[NSMutableArray alloc]initWithObjects:d1,d2,d3,d4,nil];
    }
    return _peopleCellArray;
}

- (NSMutableDictionary *)tempDict{
    if (_tempDict == nil) {
        _tempDict = [NSMutableDictionary dictionary];
    }
    return _tempDict;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.infoTableView.delegate = self;
    self.infoTableView.dataSource = self;
    self.infoTableView.scrollEnabled = NO;//不让滑动
    UINib *nib = [UINib nibWithNibName:@"PeopleInfoCell" bundle:[NSBundle mainBundle]];
    [self.infoTableView registerNib:nib forCellReuseIdentifier:@"peopleCell"];
    //右侧保存按钮
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(savePeopleInfo)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}


//保存信息
- (void)savePeopleInfo{
    PeopleDataManager *peopleDM = [[PeopleDataManager alloc]init];
    if ([self.tempDict objectForKey:kName]) {
        [peopleDM addPeopleInfoName:[self.tempDict objectForKey:kName]
                                Sex:nil
                              Title:[self.tempDict objectForKey:kTitle]
                          DateStart:nil
                            DateEnd:nil
                         BirthPlace:[self.tempDict objectForKey:kBirthPlace]
                        PeopleStory:nil
                                Son:nil
                           Daughter:nil
                            Brother:nil
                             Sister:nil
                        PeopleImage:nil
                           Comments:nil
                           PeopleID:nil];
        HHAlertView *alertview = [[HHAlertView alloc] initWithTitle:@"添加成功" detailText:@"啦啦啦" cancelButtonTitle:nil otherButtonTitles:@[@"确定"]];
        [alertview setEnterMode:HHAlertEnterModeTop];
        [alertview setLeaveMode:HHAlertLeaveModeBottom];
        [alertview showWithBlock:^(NSInteger index) {
        }];
    }
    //测试代码
    NSArray *arr = [peopleDM quaryAllPeopleInfo];
    for (People *p in arr) {
        NSLog(@"姓名:%@\n字号:%@\n",p.name,p.title);
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

#pragma mark UITableView


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return self.peopleCellArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
//    NSLog(@"%ld",indexPath.row); 0 1 2 3
    PeopleInfoCell *cell = [self.infoTableView dequeueReusableCellWithIdentifier:@"peopleCell" forIndexPath:indexPath];
    cell.selectionStyle = NO;//不让选中单元格
    NSDictionary *dict = self.peopleCellArray[indexPath.row];
    cell.titleLabel.text = dict[@"title"];
    cell.textField.placeholder = dict[@"placeholder"];
    //获取到单元格上的内容
    UITextField *theTextField =  cell.textField;
    theTextField.tag = indexPath.row;
    //给单元格text添加监听
    [theTextField addTarget:self action:@selector(textFieldWithText:) forControlEvents:UIControlEventEditingChanged];
    return cell;
}

//获取到单元格上的内容
- (void)textFieldWithText:(UITextField *)textField
{
//    NSLog(@"%ld",textField.tag);
    NSString *name = [NSString string];
    NSString *title = [NSString string];
    NSString *birthPlace = [NSString string];
    switch (textField.tag) {
        case 0:{
            name = textField.text;
            [self.tempDict setObject:name forKey:kName];
        }break;
        case 1:{
            title = textField.text;
            [self.tempDict setObject:title forKey:kTitle];
        }break;
        case 3:{
            birthPlace = textField.text;
            [self.tempDict setObject:birthPlace forKey:kBirthPlace];
        }break;
        default:break;
    }
//    NSLog(@"%@",self.tempDict);
}

#pragma mark TouchBegain
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}


@end
